package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Reply;
import com.hs.entity.User;
import com.hs.service.UserService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping
    public R addUser(@RequestBody User user) {
        int i = userService.addUser(user);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @PutMapping
    public R updateUser(@RequestBody User user) {
        int i = userService.updateUser(user);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @DeleteMapping("{id}")
    public R deleteUser(@PathVariable("id") int id) {
        int i = userService.deleteUser(id);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @GetMapping("{id}")
    public R getUserById(@PathVariable("id") int id) {
        User user = userService.getUserById(id);
        if (user != null) {
            return R.ok(user);
        }
        return R.fail("错误");
    }
    @GetMapping("page")
    public R getUserByPage(
                           @RequestParam(required = false,defaultValue = "1") int pageNum ,
                           @RequestParam(required = false,defaultValue = "2") int pageSize) {
        PageInfo<User> userByPage = userService.getUserByPage(pageNum, pageSize);
        return R.ok(userByPage);
    }
    @GetMapping
    public R login(String account,String password) {
        User userByLogin = userService.getUserByLogin(account, password);
        if (userByLogin != null) {
            return R.ok(userByLogin);
        }else {
            return R.fail("账号或密码错误");
        }
    }
    @PutMapping("updatepassword")
    public R updatePassword(@RequestBody User user) {
        int i = userService.updateUserGai(user);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @GetMapping("phone")
    public R getUserByPhone(@RequestParam("phone") String phone) {
        User user = userService.getUserByPhone(phone);
        if (user != null) {
            return R.ok(user);

        }else {
            return R.fail("失败");
        }
    }
    @PostMapping("updatephone")
    public R updatePhone(
            @RequestParam Long userId,
            @RequestParam String newPhone) {

        // 调用Service处理所有业务逻辑
        String result = userService.updatePhone(userId, newPhone);

        // 根据Service返回结果构建响应
        if ("success".equals(result)) {
            return R.ok("手机号更新成功");
        } else {
            return R.fail(result);
        }
    }
    @GetMapping("userId/{id}")
    public R getReplyByUserId(@PathVariable("id") int id) {
        List<Reply> replyById = userService.findReplyById(id);
        return R.ok(replyById);
    }
    @GetMapping("getReplyByPage")
    public R getReplyByPage(
            @RequestParam(required = false,defaultValue = "1") int pageNum ,
            @RequestParam(required = false,defaultValue = "5") int pageSize){
        PageInfo<Reply> replyByPage = userService.findReplyByPage(pageNum, pageSize);
        return R.ok(replyByPage);
    }
}
